package main

import (
	mysort "3Party/algorithm/sort"
	"fmt"
	"math/rand"
	gosort "sort"
	"time"
)

type value struct {
	v uint32
}

/*
func (me value) Less(other value) bool {
	return me.v < other.v
}*/

type sortdata []value

func (this sortdata) Less(i, j int) bool {
	l := this.Len()
	if i > l || j > l {
		return false
	}
	return this[i].v < this[j].v
}

func (this sortdata) Len() int {
	return len(this)
}

func (this sortdata) Swap(i, j int) {
	this[i], this[j] = this[j], this[i]
}

func (this sortdata) Index(i int) interface{} {
	return this[i]
}

func (this sortdata) Copy(i int, v interface{}) {
	this[i] = v.(value)
}

func test() uint32 {
	defer func() {
		fmt.Printf("defer...\n")
	}()

	return func() uint32 {
		fmt.Printf("return...\n")
		return 1
	}()
}

var num int = 3000

func main() {
	slice1 := make([]value, 0, num)
	for i := 0; i < num; i++ {
		num := uint32(rand.Intn(100000))
		slice1 = append(slice1, value{v: num})
	}

	//fmt.Println(slice1)
	fmt.Printf("-- my fast sort start -- \n")
	time1 := time.Now()
	mysort.FastSort(sortdata(slice1), 0, len(slice1)-1)
	duration1 := time.Now().Sub(time1)
	//fmt.Println(slice1)
	fmt.Printf("my fast sort time:%s\n", duration1.String())

	/////////////////////////////////////////////////////////

	slice2 := make([]value, 0, num)
	for i := 0; i < num; i++ {
		num := uint32(rand.Intn(100000))
		slice2 = append(slice1, value{v: num})
	}

	time2 := time.Now()
	fmt.Printf("-- go fast sort start -- \n")
	//fmt.Println(slice2)
	gosort.Sort(sortdata(slice2))
	duration2 := time.Now().Sub(time2)
	//fmt.Println(slice2)
	fmt.Printf("go fast sort time:%s\n", duration2.String())

	/////////////////////////////////////////////////////////

	slice3 := make([]value, 0, num)
	for i := 0; i < num; i++ {
		num := uint32(rand.Intn(100000))
		slice3 = append(slice1, value{v: num})
	}

	result := make([]interface{}, len(slice3), len(slice3))
	time3 := time.Now()
	fmt.Printf("-- my merge sort start -- \n")
	//fmt.Println(slice3)
	mysort.MergeSort(sortdata(slice3), result, 0, len(slice3)-1)
	duration3 := time.Now().Sub(time3)
	fmt.Println(slice3)
	fmt.Printf("my merge sort time:%s\n", duration3.String())
	//test()
}
